import {
	messages,
	DEFAULT_LANGUAGE
} from '@/language'

export default {
	namespaced: true,
	state: () => ({
		lang: DEFAULT_LANGUAGE,
		messages
	}),
	mutations: {
		SET_LANGUAGE(state, lang) {
			if (state.messages[lang]) {
				state.lang = lang
			}
		}
	},
	actions: {
		changeLanguage({
			commit
		}, lang) {
			commit('SET_LANGUAGE', lang)
			localStorage.setItem('user_lang', lang)
		},
		initializeLanguage({
			commit
		}) {
			const savedLang = localStorage.getItem('user_lang')
			if (savedLang) {
				commit('SET_LANGUAGE', savedLang)
			}
		}
	},
	getters: {
		currentLang: state => state.lang,
		t: state => key => state.messages[state.lang][key] || key
	}
}